package com.spring.mvc.http.jcex;

public class CryptorHMAC {
	public static final String HMAC_KEY = "http://www.swlc.sh.cn";
	private CryptorXMD5 hasher;

	public CryptorHMAC() {
		this.hasher = new CryptorXMD5();
	}

	public byte[] digestXMAC(String source) {
		try {
			return digestXMAC("http://www.swlc.sh.cn".getBytes(), source
					.getBytes("GB2312"));
		} catch (Throwable th) {
			return new byte[0];
		}
	}

	public byte[] digestXMAC(String key, String source) {
		try {
			return digestXMAC(key.getBytes(), source.getBytes("GB2312"));
		} catch (Throwable th) {
			return new byte[0];
		}
	}

	public byte[] digestXMAC(byte[] rawkey, byte[] source) {
		byte[] mackey = new byte[64];

		if (rawkey.length > mackey.length)
			mackey = this.hasher.digest(rawkey);
		else {
			System.arraycopy(rawkey, 0, mackey, 0, rawkey.length);
		}

		byte[] innerkey = new byte[mackey.length + source.length];

		for (int idx = 0; idx < mackey.length; ++idx) {
			innerkey[idx] = (byte) (mackey[idx] ^ 0x36);
		}

		System.arraycopy(source, 0, innerkey, mackey.length, source.length);

		byte[] innerhash = this.hasher.digest(innerkey);

		byte[] outerkey = new byte[mackey.length + innerhash.length];

		for (int idx = 0; idx < mackey.length; ++idx) {
			outerkey[idx] = (byte) (mackey[idx] ^ 0x5C);
		}

		System.arraycopy(innerhash, 0, outerkey, mackey.length,
				innerhash.length);

		return this.hasher.digest(outerkey);
	}
}